/**
 * 
 */
package org.dragonfire.util;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Darth Android
 * @date Jul 12, 2007
 */
public class DArray {

	public static <T> T[] subArray(T[] array, int start, int end) {
		if (array == null || start < 0 || end > array.length || end < start)
			throw new IllegalArgumentException("array is null or start/end are out of bounds.");
		ArrayList<T> ret = new ArrayList<T>();
		int count = 0;
		for (int i = start; i < end; i++) {
			ret.add(array[i]);
		}
		return ret.toArray((T[]) Array.newInstance(array.getClass().getComponentType(), ret.size()));
	}

	public static byte[] byteArray(Byte[] array) {
		byte[] b = new byte[array.length];
		for (int i = 0; i < array.length; i++) {
			b[i] = array[i];
		}
		return b;
	}

	public static Byte[] byteArray(byte[] array) {
		Byte[] b = new Byte[array.length];
		for (int i = 0; i < array.length; i++) {
			b[i] = array[i];
		}
		return b;
	}

	public static <T> T[] augment(T[] array, T[] addition) {
		List<T> list = Arrays.asList(array);
		list.addAll(Arrays.asList(addition));
		return list.toArray((T[]) Array.newInstance(array.getClass(), list.size()));
	}
}
